package com.example.eduservice.controller;

import com.example.common.commonsutils.R;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.vo.ChapterVO;
import com.example.eduservice.service.EduChapterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@Tag(name = "章节控制器")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Resource
    private EduChapterService chapterService;
    /**
     * 根据课程id查询对应的章节
     * @param courseId
     * @return
     */
    @Operation(description = "根据课程id查询对应的章节")
    @GetMapping("/course/{courseId}")
    public R queryChapterByCourseId(@PathVariable("courseId") String courseId){
        List<ChapterVO> result = chapterService.queryChapterByCourseId(courseId);
        return R.ok().data("chapterList", result);
    }
    @Operation(description = "添加章节")
    @PostMapping("/")
    public R saveChapter(@RequestBody EduChapter chapter){
        boolean save = chapterService.save(chapter);
        if (save == true){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @Operation(description = "修改章节")
    @PutMapping("/")
    public R modifyChapter(@RequestBody EduChapter chapter){
        boolean update = chapterService.updateById(chapter);
        if (update == true){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @Operation(description = "根据chapterId查询章节")
    @GetMapping("/{chapterId}")
    public R queryChapterById(@PathVariable("chapterId") String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }
    @Operation(description = "根据id删除章节")
    @DeleteMapping("/{chapterId}")
    public R removeChapterById(@PathVariable("chapterId") String chapterId){
        boolean remove = chapterService.removeChapterById(chapterId);
        if (remove == true){
            return R.ok();
        }
        return R.error();
    }
}
